package com.fsnbigdata.service.impl;

import com.fsnbigdata.domain.security.User;
import com.fsnbigdata.repository.UserRepository;
import com.fsnbigdata.service.api.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by fan.jin on 2016-10-15.
 */

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository userRepository;

    @Override
    public User findByUsername(String username ) throws UsernameNotFoundException {
        User u = userRepository.findByUsername( username );
        return u;
    }

    @Override
    public User save(User user) {
        return userRepository.saveAndFlush(user);
    }

    public User findById(Long id ) throws AccessDeniedException {
        User u = userRepository.findOne( id );
        return u;
    }

    public List<User> findAll() throws AccessDeniedException {
        List<User> result = userRepository.findAll();
        return result;
    }

    @Override
    public Long getAllImage() {
        return userRepository.getAllParkDistrictImage()
                +userRepository.getAllByImageEcosystem()
                +userRepository.getAllImageByPark()
                +userRepository.getAllImageByspecimen()
                +userRepository.getAllByImageEndngeredConserviation()
                +userRepository.getAllImageByVegetation()
                +userRepository.getAllImageEnterprise()
                +userRepository.getAllImageByOrangism()
                +userRepository.getAllImageByProduct()
                +userRepository.getAllByImagePlantConserviation();
    }

    /**
     *限制登录次数
     */
    @Override
    public int getUserStatus(String username,String password) {

        User user = userRepository.findByUsername(username);
        if(user!=null){
            BCryptPasswordEncoder bCryptPasswordEncoder =  new BCryptPasswordEncoder();
            //获取上一次的登陆时间
            Date loginTime = user.getLoginTime();
            //获取当前时间
            Date nowTime = new Date();
            //如果允许登陆时间为空或者当前登陆时间大于允许登陆时间
            if(loginTime==null||nowTime.getTime()>=(loginTime.getTime()+10*60*1000)){
                //重置登录次数
                user.setLoginTime(new Date());
                user.setLoginCount(1);
                //密码验证
                if(bCryptPasswordEncoder.matches(password,user.getPassword())){
                    //重置登录次数
                    user.setLoginCount(0);
                    //保存修改的数据
                    userRepository.saveAndFlush(user);
                    return 200;
                }
                //密码验证不通过
                userRepository.saveAndFlush(user);
                return user.getLoginCount();
            }
            //如果登录失败次数大于或等于5次
            if(user.getLoginCount()>=5){
                return -1;
            }
            //验证密码
            if(bCryptPasswordEncoder.matches(password,user.getPassword())){
                //重置登录次数
                user.setLoginCount(0);
                //保存修改的数据
                userRepository.saveAndFlush(user);
                return 200;
            }
            user.setLoginCount(user.getLoginCount()+1);
            user.setLoginTime(new Date());
            userRepository.saveAndFlush(user);
            //密码错误
            return user.getLoginCount();
        }
        //未找到该用户
        return -2;
    }

    /**
     * 根据邮箱查询用户
     * @param email
     * @return
     */
    @Override
    public List queryByEmail(String email) {
        return userRepository.queryByEmail(email);
    }
}
